package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.web.exceptions.BusinessException;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.prefs.BackingStoreException;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author xuecheng
 */
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService  companyService;

    @GetMapping("/mine")
    public ResponseResult mine(@RequestHeader("Authorization") String token) {
        //1.验证token，判断用户是否登录
        Boolean ves = JwtUtils.verifyToken(token);
        if (!ves) {
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
    //2.获取token 获取其中companyid属性 （机构id）
        Claims claims = JwtUtils.parserToken(token).getBody();
        Long companyId = claims.get("companyId", Long.class);


              //  ("companyId", long.class);
        //4.调用service 根据的查询
        Company byId = companyService.getById(companyId);
        //4.构造返回 ，BeanHelper (只能处理同名，同类型数据）
        CompanyVo companyVo = BeanHelper.copyProperties(byId, CompanyVo.class);
        return ResponseResult.okResult(companyVo);
    }
}
